package com.sf.hackathons.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sf.hackathons.app.R;
import com.sf.hackathons.app.bean.StoreDetailBean;
import com.sf.hackathons.app.presenter.constract.MainConstract;
import com.sf.hackathons.app.presenter.impl.MainPresenter;
import com.sf.hackathons.app.ui.adapter.HorPersonAdapter;
import com.sf.hackathons.app.utils.CommUtils;
import com.sf.hackathons.lib.commui.base.BaseActivity;
import com.sf.hackathons.lib.ui.HorizontalListView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 激活详情页面
 * Created by 01084347 on 2017/12/15.
 */

public class MainActivity extends BaseActivity<MainConstract.View, MainConstract.Presenter> implements MainConstract.View {

    @BindView(R.id.tv_base_price_weight)
    TextView tvBasePriceWeight;
    @BindView(R.id.tv_use_require)
    TextView tvUseRequire;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.tv_weight_count)
    TextView tvWeightCount;
    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_left_num)
    TextView tvLeftNum;
    @BindView(R.id.tv_join_num)
    TextView tvJoinNum;
    @BindView(R.id.mHorListView)
    HorizontalListView mHorListView;
    @BindView(R.id.tv_progress_bar)
    TextView tvProgressBar;
    @BindView(R.id.rl_join)
    RelativeLayout rlJoin;
    @BindView(R.id.rl_total)
    RelativeLayout rlTotal;
    private StoreDetailBean mDetailBean = new StoreDetailBean();
    private HorPersonAdapter mHorPersonAdapter;


    @OnClick({R.id.btn_submit, R.id.iv_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                Intent mIntent = new Intent(MainActivity.this, MailInfoActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putInt("groupId", mDetailBean.getGroupId());
                mBundle.putString("groupName", mDetailBean.getGroupName());
                mBundle.putDouble("weightMax", mDetailBean.getWeightMax());
                mBundle.putDouble("weightMin", mDetailBean.getWeightMin());
                changeActivity(mIntent);
                break;
        }
    }


    @Override
    protected int initContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        setHeaderTitle(getString(R.string.main_title));
        setRightImgResId(R.drawable.delete_selector, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, WeChatCaptureActivity.class);
                startActivity(intent);
            }
        });
        getPresenter().loadHttpDetail();
    }

    @Override
    protected void initProcess(Bundle savedInstanceState) {


    }

    @Override
    public void loadDetailSuccess(StoreDetailBean mStoreDetailBean) {
        mDetailBean = mStoreDetailBean;

        tvTitle.setText(mDetailBean.getGroupName());

        String weightAndCount = String.format(getResources().getString(R.string.main_weight_and_count), mDetailBean.getWeightMin(), mDetailBean.getWeightMax(), mDetailBean.getDailyMinPackages());
        tvWeightCount.setText(weightAndCount);

        String basePriceWeight = String.format(getResources().getString(R.string.main_base_price_weight), mDetailBean.getBasePrice(), mDetailBean.getBaseWeight());
        tvBasePriceWeight.setText(basePriceWeight);

        CommUtils.loadImage(MainActivity.this, ivImage, mDetailBean.getBannarImgs().get(0));

        String[] strTime = mDetailBean.getEndTime().split("-");
        String newEndTime = strTime[1] + "月" + strTime[2] + "日";
        String endTime = String.format(getResources().getString(R.string.main_end_time), newEndTime);
        tvEndTime.setText(endTime);

        tvUseRequire.setText(Html.fromHtml(mDetailBean.getUseRequire()));

        String leftNum = String.format(getResources().getString(R.string.main_remain_person), mDetailBean.getLeftNum());
        tvLeftNum.setText(leftNum);
        tvLeftNum.setTextColor(getResources().getColor(R.color.color_10));


        String joinNum = String.format(getResources().getString(R.string.main_join_person), mDetailBean.getJoinNum());
        tvJoinNum.setText(joinNum);
        tvJoinNum.setTextColor(getResources().getColor(R.color.color_10));
        initHorListView();

        int newJoinNum = mDetailBean.getJoinNum();
        int newTotalNum = mDetailBean.getGroupLimit();
        double rate = newJoinNum / newTotalNum;

        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) rlJoin.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.width = 200 * newTotalNum;
        rlJoin.setLayoutParams(linearParams);
        tvProgressBar.setText(rate + "");
    }


    @Override
    public void loadDetailFail(String errCode, String errMsg) {

    }


    @NonNull
    @Override
    public MainConstract.Presenter createPresenter() {
        return new MainPresenter();
    }

    public void initHorListView() {
        mHorPersonAdapter = new HorPersonAdapter(MainActivity.this, mDetailBean.getUsers());
        mHorListView.setAdapter(mHorPersonAdapter);
        mHorListView.setFocusable(false);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
